/*
-----------------------------------------------------------------------------
Filename:    TutorialApplication.cpp
-----------------------------------------------------------------------------

This source file is part of the
   ___                 __    __ _ _    _ 
  /___\__ _ _ __ ___  / / /\ \ (_) | _(_)
 //  // _` | '__/ _ \ \ \/  \/ / | |/ / |
/ \_// (_| | | |  __/  \  /\  /| |   <| |
\___/ \__, |_|  \___|   \/  \/ |_|_|\_\_|
      |___/                              
      Tutorial Framework
      http://www.ogre3d.org/tikiwiki/
-----------------------------------------------------------------------------
*/
#include "TutorialApplication.h"

#include <btBulletDynamicsCommon.h>

//btDiscreteDynamicsWorld* dynamicsWorld;
//btDefaultCollisionConfiguration* collisionConfiguration;
//btCollisionDispatcher* dispatcher;
//btSequentialImpulseConstraintSolver* solver;

OgreCanvas * gc;
int shadow_i;
using namespace Ogre;

//-------------------------------------------------------------------------------------
TutorialApplication::TutorialApplication(void)
{
	m_pTimer = OGRE_NEW Ogre::Timer(); 
	m_pTimer->reset();
	timeOld = m_pTimer->getMillisecondsCPU();
}
//-------------------------------------------------------------------------------------
TutorialApplication::~TutorialApplication(void)
{
	// Clean up behind ourselves like good little programmers

	delete gc;
	delete m_pTimer;
	//delete broadphase;
}

//-------------------------------------------------------------------------------------

void TutorialApplication::createScene(void)
{
    // create your scene here :)
	m_pCurrentScene = new Stage_1(mSceneMgr,mCamera);
	m_pCurrentScene->init();
	m_pCurrentScene->load();


	Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("mouse_mat", "General");
	Ogre::TextureUnitState *tex = material->getTechnique(0)->getPass(0)->createTextureUnitState("shadow.png");
	//tex->setTextureFiltering(TFO_NONE);
	material->getTechnique(0)->getPass(0)->setDepthCheckEnabled(true);
	material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false);
	material->getTechnique(0)->getPass(0)->setLightingEnabled(false);
	material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
	m_pMouseNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	Ogre::Entity * _ent = mSceneMgr->createEntity("yumi.mesh");
	m_pMouseNode->attachObject(_ent);
	_ent->setMaterialName("mouse_mat");
	m_pMouseNode->setScale(.1,.1,.1);

	//CompositorManager::getSingleton().addCompositor(mWindow->getViewport(0), "Bloom");
	//Ogre::CompositorManager::getSingleton().setCompositorEnabled(mWindow->getViewport(0),"Bloom",true);
	
	gc = new OgreCanvas();
	gc->init(mSceneMgr,mCamera,Ogre::RENDER_QUEUE_OVERLAY,false);

	shadow_i = 0;
	


}

void TutorialApplication::update()
{
	timeCurrent = m_pTimer->getMillisecondsCPU();
	Ogre::Real tmp = timeOld;
	timeSinceLast = timeCurrent - timeOld;
	timeOld = timeCurrent;
	//////////////////////////
	m_pCurrentScene->update(timeSinceLast/1000);
	//////////////////////////
	gc->setColor(0xffffffff);

	//lock framerate
	Ogre::Real sleepTime = ((tmp + 14700) - m_pTimer->getMillisecondsCPU())/1000;
	if(sleepTime > 0)
		Sleep(sleepTime);
}

bool TutorialApplication::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
	m_pCurrentScene->eventClick(arg.state.X.abs, arg.state.Y.abs);
	return true;
}
bool TutorialApplication::mouseMoved( const OIS::MouseEvent &arg )
{
	
	//adjust camera
	if(arg.state.X.abs > SCREEN_WIDTH - 10)
		mCamera->move(Vector3(1,0,0));
	if(arg.state.X.abs < 10)
		mCamera->move(Vector3(-1,0,0));
	if(arg.state.Y.abs > SCREEN_HEIGHT - 10)
		mCamera->move(Vector3(0,0,1));
	if(arg.state.Y.abs < 10)
		mCamera->move(Vector3(0,0,-1));
	
	
	
	Ogre::Vector3 p3D = Sprite::_getInteractPosition3D(mCamera,Ogre::Vector2(arg.state.X.abs, arg.state.Y.abs),0);
	m_pMouseNode->setPosition(p3D);

	m_pCurrentScene->eventMouseMove(arg.state.X.abs, arg.state.Y.abs, 
		arg.state.X.rel, arg.state.Y.rel,
		arg.state.buttonDown(OIS::MB_Left));

	return true;
}

bool TutorialApplication::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
	m_pCurrentScene->eventMouseRelease(arg.state.X.abs, arg.state.Y.abs);

	return true;
}




#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
    int main(int argc, char *argv[])
#endif
    {
        // Create application object
        TutorialApplication app;

        try {
            app.go();
        } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
            MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
            std::cerr << "An exception has occured: " <<
                e.getFullDescription().c_str() << std::endl;
#endif
        }

        return 0;
    }

#ifdef __cplusplus
}
#endif
